package com.ate.ui.fragments2;

import android.content.Context;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.ate.adapters.OnFragmentRecyAdp;
import com.ate.bean.SelBean;
import com.ate.myview.MyScrollView;
import com.ate.ui.GoodsActivity;
import com.ate.adapters.MyRecyAdapter;
import com.ate.javamvp.R;
import com.ate.bean.GoodsBean;
import com.ate.javamvp.mvp.contract.Contract;
import com.ate.javamvp.mvp.presenter.DataPresneter;
import com.ate.lib_core.http.mvp.view.BaseFragment;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class OneFragment extends BaseFragment implements Contract.DataXiaView {


    private List<String> list_pic;
    private DataPresneter dataPresneter;
    private MyScrollView myscroll;
    private SmartRefreshLayout smartFragone;


    @Override
    public int bandlayout() {
        return R.layout.fragment_one;
    }

    private RecyclerView recy;
    private MyRecyAdapter myRecyAdapter;
    private Banner banner;

    @Override
    public void initView() {
        recy = (RecyclerView) findViewById(R.id.recy);
        banner = (Banner) findViewById(R.id.banner);
        myscroll = (MyScrollView) findViewById(R.id.myscroll);
        smartFragone = (SmartRefreshLayout) findViewById(R.id.smart_fragone);

        myscroll.setOnScrollChangeListener(new MyScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(MyScrollView view, int x, int y, int oldx, int oldy) {
//                Log.e("a", "" + y);
//                if (y>40){
//                    smartFragone.setEnableRefresh(false);
//                }else {
//                    smartFragone.setEnableRefresh(true);
//                }
            }

            @Override
            public void onScrollBottomListener() {

            }

            @Override
            public void onScrollTopListener() {

            }
        });
    }


    @Override
    public void initData() {

        //推荐
        dataPresneter = new DataPresneter(OneFragment.this);
        dataPresneter.getGoods();

        //轮播图
        addBanner();

    }

    private void addBanner() {
        list_pic = new ArrayList<>();
        list_pic.add("http://www.qubaobei.com/ios/cf/uploadfile/132/11/10018.jpg");
        list_pic.add("http://www.qubaobei.com/ios/cf/uploadfile/132/6/5654.jpg");
        list_pic.add("http://www.qubaobei.com/ios/cf/uploadfile/132/4/3751.jpg");
        list_pic.add("http://www.qubaobei.com/ios/cf/uploadfile/132/10/9961.jpg");
        list_pic.add("http://www.qubaobei.com/ios/cf/uploadfile/132/3/2330.jpg");
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                String pa = (String) path;
                Glide.with(getContext()).load(pa).into(imageView);
            }
        });
        banner.setImages(list_pic);
        banner.start();

    }

    @Override
    public void onSuccess(List<GoodsBean.DataBean> data) {
        myRecyAdapter = new MyRecyAdapter(R.layout.layout, data);
        recy.setAdapter(myRecyAdapter);
        recy.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));

        myRecyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                GoodsBean.DataBean item = (GoodsBean.DataBean) adapter.getItem(position);
                Bundle bundle = new Bundle();
                bundle.putSerializable("item", item);
                Intent intent = new Intent(getContext(), GoodsActivity.class);

                intent.putExtra("item", bundle);
                startActivity(intent);
            }
        });
        gettablayout();
    }

    private List<SelBean> list = new ArrayList<>();

    private void gettablayout() {
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.onfrgament_recy);
        list.add(new SelBean("推荐", true));
        list.add(new SelBean("球鞋", false));
        list.add(new SelBean("特惠", false));
        list.add(new SelBean("男装", false));
        list.add(new SelBean("高街", false));
        list.add(new SelBean("女装", false));
        list.add(new SelBean("生活", false));
        OnFragmentRecyAdp adapter = new OnFragmentRecyAdp(R.layout.layout_sel, list);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false));
        adapter.notifyDataSetChanged();

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (SelBean selBean : list) {
                    selBean.setSelect(false);
                }
                list.get(position).setSelect(true);
                adapter.notifyDataSetChanged();

            }
        });


    }

    @Override
    public void onFail() {

    }
}